package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.util.HierarchyElementUtil;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The complex type hierarchy is used by most or all the hierarchical elements of act-like documents.
 * 
 * <pre>
 * 	<xsd:complexType name="hierarchy">
 * 		<xsd:complexContent>
 * 			<xsd:extension base="basehierarchy">
 * 				<xsd:choice>
 * 					<xsd:sequence>
 * 						<xsd:element ref="intro" minOccurs="0" maxOccurs="1"/>
 * 							<xsd:choice minOccurs="1" maxOccurs="unbounded">
 * 								<xsd:element ref="componentRef"/>
 * 							    <xsd:group ref="hierElements"/>
 * 						    </xsd:choice>
 * 						    <xsd:element ref="wrap" minOccurs="0" maxOccurs="1"/>
 * 						</xsd:sequence>
 * 					    <xsd:element ref="content"/>
 * 				</xsd:choice>
 * 				<xsd:attributeGroup ref="corereq"/>
 * 			</xsd:extension>
 * 		</xsd:complexContent>
 * 	</xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public abstract class Hierarchy extends BaseHierarchyCoreReq {

	private Content _content;

	// or
	private Intro _intro;
	private List<HierarchyElement> _elements;
	private Wrap _wrap;

	public Content getContent() {
		return _content;
	}

	/**
	 * @see org.akomantoso.core.data.BaseHierarchy#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		super.writeExternal(out);

		if (_content != null) {
			_content.writeExternal(out);
		} else {
			if (_intro != null) {
				_intro.writeExternal(out);
			}
			for (int i = 0, n = _elements.size(); i < n; i++) {
				_elements.get(i).writeExternal(out);
			}
			if (_wrap != null) {
				_wrap.writeExternal(out);
			}
		}

	}

	/**
	 * @see org.akomantoso.core.data.BaseHierarchy#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		final String localName = in.getLocalName();

		super.readExternal(in);

		if (Content.ELEMENT == in.getLocalName()) {
			_content = new Content();
			_content.readExternal(in);
		} else {
			if (Intro.ELEMENT == in.getLocalName()) {
				_intro = new Intro();
				_intro.readExternal(in);
			}

			HierarchyElementUtil.read(in, _elements = new ArrayList<HierarchyElement>(4), localName);

			if (Wrap.ELEMENT == in.getLocalName()) {
				_wrap = new Wrap();
				_wrap.readExternal(in);
			}

		}
	}

	/**
	 * @see org.akomantoso.core.data.BaseHierarchy#internalAccept(org.akomantoso.core.visitor.AKVisitor)
	 */
	@Override
	protected void internalAccept(AKVisitor visitor) {
		super.internalAccept(visitor);

		if (_content != null) {
			_content.accept(visitor);
		} else {
			if (_intro != null) {
				// _intro.accept(visitor);
			}
			if (_elements != null) {
				for (int i = 0, n = _elements.size(); i < n; i++) {
					_elements.get(i).accept(visitor);
				}
			}
			if (_wrap != null) {
				// _wrap.accept(visitor);
			}
		}

	}

}